package models

import (
	"LoveU/utils"
	// "github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)

// 获得名称含有pattern的所有用户的Slice
func GetUsersByName(pattern string) []*User {
	o := orm.NewOrm()
	qs := o.QueryTable("user")
	users := make([]*User, 0)
	_, err := qs.Filter("name__contains", pattern).All(&users, "id", "name")
	if err != nil {
		return nil
	}
	return users
}

// 获得名称含有pattern的用户分页列表（第page页，一页pagesize项）
func GetUserListByName(pattern string, page, pagesize int64) *utils.Pagination {
	users := GetUsersByName(pattern)
	if users == nil {
		return nil
	}
	sum := int64(len(users))
	startLine := (page - 1) * pagesize
	var pageCount,limit int64
	var results = make([]*User, 0)
	if sum%pagesize == 0 {
		pageCount = sum / pagesize
	} else {
		pageCount = sum/pagesize + 1
	}
	//控制下标
	if startLine >= sum {
		limit = 0
	} else if startLine+pagesize > sum {
		limit = sum%pagesize
		results = users[startLine : startLine+limit]
	} else {
		limit = pagesize
		results = users[startLine : startLine+limit]
	}
	data := &utils.Pagination{
		Page:      page,
		Pagesize:  pagesize,
		Pagecount: pageCount,
		Sum:       sum,
		Data:      results,
	}
	return data
}

// 为“缘分搜索”获得除自己外所有用户的资料
func getAllUsersInfoExceptMe(userGentle int, myUserid,page,pagesize int64) (int64,[]*Pinformation) {
	o := orm.NewOrm()
	qs := o.QueryTable("pinformation").Exclude("userid",myUserid)
	users := make([]*Pinformation, 0)
	startLine := (page - 1) * pagesize
	var count64 int64
	if userGentle==DEFAULT_GENTLE {
		count, err := qs.Limit(pagesize).Offset(startLine).All(&users)
		if err != nil {
			return -1,nil
		}
		count64 = int64(count)
	} else {
		count, err := qs.Exclude("gentle",userGentle).Limit(pagesize).Offset(startLine).All(&users)
		if err != nil {
			return -1,nil
		}
		count64 = int64(count)
	}
	return count64,users
}

// 为“缘分搜索”获得指定页码、数量之用户的评分
func GetUsersScore(gentle int, userid,page,pagesize int64) *utils.Pagination {
	count,users := getAllUsersInfoExceptMe(gentle,userid,page,pagesize)
	myInfo := GetUserInfo(userid)
	countAll,err0 := orm.NewOrm().QueryTable("pinformation").Limit(-1).Count()
	if users==nil || myInfo==nil || (err0!=nil&&err0!=orm.ErrNoRows) {
		return nil
	}
	scores := make([]*UserScore,0)
	for _,userInfo := range users {
		if userInfo.Userid==myInfo.Userid {
			continue
		}
		score := new(UserScore)
		//【注意】beauty、jobscore未实现，取随机数
		score.Id = userInfo.Userid
		score.Agescore = utils.ScoreAgeUnix(userInfo.Birthday.Unix(),myInfo.Birthday.Unix())
		score.Beauty = utils.GetRandomFloat()
		score.Hobby = utils.ScoreStringAttribute(userInfo.Hobby,myInfo.Hobby)
		score.Impression = utils.ScoreStringAttribute(userInfo.Requir,myInfo.Requir)
		score.Jobscore = utils.GetRandomFloat()
		score.Residence = utils.ScoreArea(userInfo.Area,myInfo.Area)
		score.Emotion = utils.ScoreStringAttribute(userInfo.Emotion,myInfo.Emotion)
		score.Summary = 0.2*score.Agescore+0.2*score.Residence+0.2*score.Impression+
			0.1*score.Beauty+0.1*score.Hobby+0.1*score.Jobscore+0.1*score.Emotion
		score.Name = GetUser2(score.Id).Name
		score.IsVip = userInfo.Vip
		score.Avatar = userInfo.Avatar
		score.Age = utils.GetCurrentAge(userInfo.Birthday)
		score.Job = userInfo.Job
		scores = append(scores,score)
	}
	var pageCount int64
	if countAll%pagesize == 0 {
		pageCount = countAll / pagesize
	} else {
		pageCount = countAll/pagesize + 1
	}
	data := &utils.Pagination{
		Page:      page,
		Pagesize:  pagesize,
		Pagecount: pageCount,
		Sum:       count,
		Data:      scores,
	}
	return data
}